<?php
/**
 * Created by PhpStorm.
 * User: zzw
 * Date: 17-3-22
 * Time: 上午9:56
 */
class Information extends MY_Controller
{
    protected $article = array(
        array(
            'field' => 'phone',
            'label' => '帐号',
            'rules' => 'trim|required|numeric|is_mobile|xss_clean|callback_checkLoginPhone'
        ),
        array(
            'field' => 'pwd',
            'label' => '密码',
            'rules' => 'trim|required|xss_clean|callback_checkPwd'
        )
    );

    function __construct()
    {
        parent::__construct();
        $this->_auto();
        if($this->userinfo['ctype'] != 1 || !$this->userinfo['is_company']) redirect(site_url('/article'));
        $this->load->model('company_model','mcompany');
    }

    public function index()
    {
        $this->vdata['header'] = array(
            'title' => $this->mcfg->get_config('site', 'title_seo'),
            'tags' => $this->mcfg->get_config('site', 'tags'),
            'intro' => $this->mcfg->get_config('site', 'intro')
        );
        # 在使用控制器时输出CI共模板使用
        $this->vdata['CI'] =& $this;
        $this->load->model('category_model','mcategory');
        $company = $this->mcompany->get_one($this->userinfo['company']);
        $com_industry = explode(',',$company['industry']);
        $industry = $this->mcategory->get_all(array('ctype'=>24),'id,title');
        foreach($industry as &$row) {
            $row['is_select'] = in_array($row['id'],$com_industry) ? 1 : 0;
        }
        $accounts = $this->macc->get_all(array('company'=>$company['id'],'audit'=>0,'ctype'=>1,'id <>'=>$this->uid),'id,uname,position');
        $this->vdata['company'] = $company;
        $this->vdata['industry'] = $industry;
        $this->vdata['accounts'] = $accounts;
        $this->vdata['com_plan'] = $company['plan'] ? one_upload($company['plan']) : '';
        //var_dump($this->vdata['com_plan']);die();
        $this->load->view('information', $this->vdata);
    }
    public function update() {
        $data = $this->input->post();
        var_dump($data);die();
    }
    public function add() {
        $data = $this->input->post();
        $id = $data['id'];
        unset($data['id']);
        $update = $data;
        $update['uid'] = $data['transfer'];
        $update['transfer'] = 0;
        $updateid = $this->mcompany->update($update,array('id'=>$id));
        redirect(site_url('information/index'));
    }
    public function logo() {
        $vdata = array( 'status' => 0, 'msg' => '未知错误！');
        $uid=$this->session->userdata('uid');
        if (is_post()) {
            if($uid){
                $key = key($_FILES);
                $config['upload_path'] = UPLOAD_PATH.'logo/';
                $config['allowed_types'] = 'gif|jpg|png|bmp|pdf|ppt|word|zip|rar';
                $config['max_size'] =$key== 'plan' ? 20*1024*1024 : 2*1024*1024;
                /*$config['max_width']  = '500';
                $config['max_height']  = '500';*/
                $config['file_name']  = md5(time()) ;

                $this->load->library('upload', $config);
                if ((isset($_FILES[$key]) && !$this->upload->do_upload($key))) {
                    $vdata = array("status" => 2,'error' => $this->upload->display_errors());
                } else {
                    $data = array('upload_data' => $this->upload->data());
                    $user_photo = $this->mcompany->get_one(array('uid'=>$uid));
                    if($user_photo[$key] && file_exists(UPLOAD_URL.$user_photo[$key])) {
                        chmod(UPLOAD_PATH.$user_photo[$key],777);
                        unlink(UPLOAD_PATH.$user_photo[$key]);
                    }
                    if ($this->mcompany->update(array($key=>'logo/'.$data['upload_data']['file_name']), array('uid'=>$uid))) {
                        $vdata['status'] = 1;
                        $vdata['msg'] = "更新成功";
                        $vdata['url'] = UPLOAD_URL.'logo/'.$data['upload_data']['file_name'];
                    } else {
                        $vdata['status'] = 0;
                        $vdata['msg'] = "更新失败";
                    }
                }
            }else{
                $vdata['status'] = 0;
                $vdata['msg'] = "请先登录之后再进行操作";
            }
            $this->output->set_content_type('application/json')->set_output(json_encode($vdata));
        }else {
            show_404();
        }
    }
    public function delLogo() {
        $user_photo = $this->mcompany->get_one(array('uid'=>$this->uid));
        if($user_photo['photo'] && file_exists(UPLOAD_PATH.$user_photo['photo'])) {
            chmod(UPLOAD_PATH.$user_photo['photo'],777);
            unlink(UPLOAD_PATH.$user_photo['photo']);
            $vdata['status'] = 1;
            $vdata['msg'] = "删除成功";
        } else {
            $vdata['status'] = 0;
            $vdata['msg'] = "删除失败";
        }
        $this->output->set_content_type('application/json')->set_output(json_encode($vdata));
    }
    public function download() {
        $user_photo = $this->mcompany->get_one(array('uid'=>$this->uid),'plan');
        $plan = one_upload($user_photo['plan']);
        download_files(UPLOAD_PATH.$plan['url'],$plan['name']);
    }
    public function combing() {
        $this->load->model('company_model','mcompany');
        $data = $this->input->post();
        $id = $data['id'];
        $company = $this->mcompany->get_one($id,'is_combing,status');
        if($company['is_combing'] && !$company['status']) {
            $vdata['status'] = 0;
            $vdata['msg'] = '您的申请已受理，客服将尽快与您联系';
        } else {
            $is_combing = $company['is_combing'] ? 0 : 1;
            if($company['status']) {
                $is_combing = 1;
            }
            $this->mcompany->set($id,array('is_combing'=>$is_combing,'status'=>0));
            $vdata['status'] = 1;
            $vdata['msg'] = '申请成功，客服将尽快与您联系';
        }
        $this->output->set_content_type('application/json')->set_output(json_encode($vdata));
    }
}